import { defineComponent, ref, reactive, onMounted, onUnmounted } from "vue";
import TablePage from "@/components/TablePage/TablePage";
import { tablesInterface } from "@/common/interface/table.interface";
import ChordsService from "./index.service";
import styles from "./index.module.less";
import Add from "./Add/Add";
const Enter = defineComponent({
  props: {
    text: {
      type: String,
      required: true,
    },
  },
  setup(props) {
    return () => (
      <span class={[styles.Enter, props.text.toString() === '0' && styles.danger]}>
        {props.text.toString() === '1' ? "已录入" : "未录入"}
      </span>
    )
  }
})
export default defineComponent({
  name: "List",
  setup() {
    const tablePageRef = ref(null as any);
    const chordsService = new ChordsService();
    // 表格定义
    const tables = reactive({
      list: [
        {
          title: "和弦管理",
          rowKey: "id",
          columns: [
            {
              title: "ID",
              dataIndex: "id",
              align: "center",
              width: "80px",
            },
            {
              title: "和弦名称",
              dataIndex: "name",
              align: "center",
              width: "280px",
            },
            {
              title: "是否录入",
              dataIndex: "enter",
              align: "center",
              width: "280px",
            },
            {
              title: "替换和弦名称",
              dataIndex: "replaceName",
              align: "center",
              width: "280px",
            },
            {
              title: "排序",
              dataIndex: "sort",
              align: "center",
              width: "200px",
            },
          ],
          toolbar: {
            left: ["refresh", "add"],
            right: ["filter", "search"],
            add: {
              permissions: "music:songChords:add",
              handler: () => {
                const addPopupRef = tablePageRef.value.getPopup("addRef");
                addPopupRef.value.open({ type: 'add' });
              }
            },
          },
          searchForm: {
            name: {
              type: "input",
              label: "和弦名称",
              attrs: { placeholder: "和弦名称", allowClear: true },
            },
            enter: {
              type: "select",
              label: "是否录入",
              attrs: { placeholder: "是否录入", allowClear: true },
              datas: [{ label: "已录入", value: 1 }, { label: "未录入", value: 0 }],
            },
          },
          service: chordsService,
          operation: ({ record }) => [
            {
              type: "edit",
              permissions: "music:songChords:edit",
              clickHandler: () => {
                const addPopupRef = tablePageRef.value.getPopup("addRef");
                addPopupRef.value.open({ type: 'edit', id: record.id });
              }
            },
            {
              type: "delete",
              permissions: "music:songChords:remove",
              clickHandler: "delete",
            },
          ],
          bodyCell: ({ text, record, index, column }) => {
            if (column.dataIndex === "enter") {
              return <>
                <Enter text={text.toString()} />
              </>
            }
          },
          popups: [
            {
              ref: "addRef",
              dom: () => <Add onChange={() => { tablePageRef.value.reload() }} />,
            },
          ],
          pagination: true,
          tableSourceFormat: {
            data: 'rows',
            pageSize: 'pageSize',
            current: 'pageNum',
          },
        }
      ],
    } as tablesInterface)
    // 配置项
    const config = reactive({
      table: tables.list[0],
    })
    return () => (
      <TablePage ref={tablePageRef} config={config} />
    )
  }
})